package eerbaar.shared.internal.tree.commands;

import java.util.ArrayList;
import java.util.List;

import eerbaar.shared.tree.INode;
import eerbaar.shared.tree.IPayload;
import eerbaar.shared.tree.NodeIterator;

public class FindPayloadCmd {

    private final INode start;

    public FindPayloadCmd(INode start) {
        this.start = start;
    }

    public List<IPayload> execute(String name) {

        assert (this.start != null);

        List<IPayload> result = new ArrayList<IPayload>();

        NodeIterator i = new NodeIterator(this.start);
        while (i.hasNext()) {

            NodeIterator.Result r = i.next();
            INode n = r.node;
            IPayload payload = n.getPayload();
            String test = payload.getKey();
            if (test.equals(name)) {
                result.add(payload);
            }
        }

        return result;
    }

}
